package cn.com.cfae.iras.doc.analyze.parse.tokenizer;

import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.CompoundWord;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.corpus.document.sentence.word.Word;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class FXJGTokenizer extends BaseTokenizer {


    private static Logger logger = LoggerFactory.getLogger(FXJGTokenizer.class);

    private FXJGTokenizer() {
    }

    private static FXJGTokenizer instance = new FXJGTokenizer();

    public static FXJGTokenizer getInstance() {
        return instance;
    }

    private static List<String> filters = new ArrayList<>();

    static {
        filters.add("声明");
        filters.add("截至");
        filters.add("股权关系");
        filters.add("利害关系");
    }

    public ExtractItem splitWords(ExtractBizModel extractBizModel, String text) {
        return splitWords(extractBizModel, text, null);
    }

    public ExtractItem splitWords(ExtractBizModel extractBizModel, String text, ExtractItem targetExtractItem) {
        for (String filterString : filters) {
            if (text.contains(filterString)) {
                return null;
            }
        }
        if (extractBizModel == null) {
            return null;
        }
        if (StringUtils.isEmpty(text)) {
            return null;
        }
        text = replaceFXJG_Prefix(text);
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(text);
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        ExtractItem extractItem = null;
        logger.info("分析发行机构业务指标模型，分词列表：{}。", wordList);
        List<ExtractItem> extractItemList = new ArrayList<>();
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            if (iWord instanceof CompoundWord) {
                CompoundWord compoundWord = (CompoundWord) iWord;
                List<Word> innerWordList = compoundWord.innerList;
                for (int j = 0; j < innerWordList.size(); j++) {
                    IWord innerWord = innerWordList.get(j);
                    String value = innerWord.getValue().trim();
                    if (extractBizModel.getSynonyms().contains(value)) {
                        if (j >= 3) {
                            break;
                        }
                        extractItem = extractBizModel.getExtractItemBySynonym(value);
                        extractItemList.add(extractItem);
                        logger.info("分析发行机构业务指标模型，分析到业务模型及业务指标，IteName：{}，指标名称：{}。", extractItem.getItemName(), value);
                        continue;
                    }
                }
                if (extractItem != null) {
                    break;
                }
            } else {
                String value = iWord.getValue().trim();
                if (extractBizModel.getSynonyms().contains(value)) {
                    if (i >= 3) {
                        break;
                    }
                    extractItem = extractBizModel.getExtractItemBySynonym(value);
                    extractItemList.add(extractItem);
                    logger.info("分析发行机构业务指标模型，分析到业务模型及业务指标，IteName：{}，指标名称：{}。", extractItem.getItemName(), value);
                    continue;
                }
            }
        }
        return extractItem;
    }
}
